﻿using Microsoft.EntityFrameworkCore;
using MyServer.Todo.Domain;
using MyServer.Todo.Infrastructure.DatabaseContext.EntityConfigurations;

namespace MyServer.Todo.Infrastructure.DatabaseContext
{
    public class TodoDbContext:DbContext, IUnitOfWork
    {

        public virtual DbSet<TodoGroup> TodoGroups { get; set; }
        public virtual DbSet<Domain.Aggregates.TodoGroupAggregate.Todo> Todos { get; set; }
        public virtual DbSet<TodoGroupSettings> TodoGroupSettings { get; set; }
        public virtual DbSet<TodoSettings> TodoSettings { get; set; }

        public TodoDbContext(DbContextOptions options):base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new TodoConfig());
            modelBuilder.ApplyConfiguration(new TodoGroupConfig());
            modelBuilder.ApplyConfiguration(new TodoSettingsConfig());
            modelBuilder.ApplyConfiguration(new TodoGroupSettingsConfig());
        }

        public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default)
        {
            return await base.SaveChangesAsync(cancellationToken) > 0;
        }
    }
}
